﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Web.Script.Services;
using System.Web.Services;
using BikeBouncer.BusinessLayer;

namespace BikeBouncer.Site
{
	[WebService(Namespace = "http://bikebouncer.com/")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[ToolboxItem(false)]
	[ScriptService]
	public class Ajax_asmx : WebService
	{
		#region Autocomplete methods

		[WebMethod(EnableSession = true, CacheDuration = 60)]
		public String[] AutoComplete_Maker(String prefixText, Int32 count)
		{
			ArrayList result = new ArrayList();
			SqlDataReader reader = DataAccess.Maker_Fetch(prefixText);

			while (reader.Read())
			{
				result.Add(reader["MakerTitle"].ToString());
			}

			return (String[])result.ToArray(typeof(String));
		}

		[WebMethod(EnableSession = true, CacheDuration = 60)]
		public String[] AutoComplete_City(String prefixText, Int32 count)
		{
			ArrayList result = new ArrayList();
			SqlDataReader reader = DataAccess.City_Get(prefixText);

			while (reader.Read())
			{
				String c = reader["CityTitle"] + ", ";
				String region = reader["CityRegion"].ToString();

				if (!String.IsNullOrEmpty(region))
				{
					c += region + ", ";
				}

				c += reader["CityCountry"];

				result.Add(c);
			}

			return (String[])result.ToArray(typeof(String));
		}

		#endregion
	}
}